import type {
  CreateCrudOptionsProps,
  CreateCrudOptionsRet,
  CrudExpose,
} from '@fast-crud/fast-crud';

import type { FormRulesExt } from '#/api';
import type { IotBsBatchModel } from '#/api/iot/device/bsBatch/model/iotBsBatchModel';

import { ref } from 'vue';
import { useAccess } from '@vben/access';
import { $t } from '@vben/locales';
import { IotBsBatchApi } from '#/api/iot/device/bsBatch/iotBsBatch';
import { ActionEnum, DictEnum } from '#/enums/commonEnum';
import {
  backendDict,
  createdTimeColumn,
  deleteButton,
  indexColumn,
} from '#/plugins/fast-crud/common';
import {useMessage} from "#/hooks/web/useMessage";

const { hasPermission } = useAccess();
const { createMessage } = useMessage();
type ContextRef = object;
/**
 * 设备-批量-入库
 */
export function deviceImport(type: number, props: CreateCrudOptionsProps) {
  const input = document.createElement('input');
  input.type = 'file';
  input.hidden = true;
  input.addEventListener('change', async (event) => {
    const file = (event.target as HTMLInputElement).files?.item(0);
    if (file) {
      const formData = new FormData();
      formData.append('file', file);
      try {
        // 调用上传接口
        const result = await IotBsBatchApi.publicUpload(type, formData);
        if (result) {
          createMessage.success('任务创建成功');
          props.crudExpose.doRefresh();
        }
      } catch {
        createMessage.error('文件上传失败');
      }
    }
    // 清理资源：移除input元素
    input.remove();
  });
  // 添加到DOM中才能触发点击
  document.body.append(input);
  input.click();
}

export function createCrudOptions(
  props: CreateCrudOptionsProps<IotBsBatchModel.IotBsBatchResultVO, ContextRef>,
): CreateCrudOptionsRet<IotBsBatchModel.IotBsBatchResultVO> {
  const selectedRowKeys = ref<string[]>([]);
  return {
    crudOptions: {
      request: {
        pageRequest: IotBsBatchApi.pageRequest,
        addRequest: IotBsBatchApi.addRequest,
        editRequest: IotBsBatchApi.editRequest,
        delRequest: IotBsBatchApi.delRequest,
        infoRequest: IotBsBatchApi.infoRequest,
      },
      actionbar: {
        buttons: {
          add: {
            show: hasPermission('iot:iot:iotBsBatch:add'),
          },
          ...deleteButton({
            crudExpose: props.crudExpose,
            selectedRowKeys,
            removeFn: IotBsBatchApi.remove,
            role: 'iot:iot:iotBsBatch:delete',
          }),
          deviceImportFile: {
            show: hasPermission('batch:device:import'),
            text: $t('iot.device.bsBatch.iotBsBatchButten.importNname'),
            icon: 'ant-design:cloud-upload',
            click() {
              deviceImport(1,props);
            },
            tooltip: {
              // tooltip的属性
              title: '上传模板请到按钮旁边下载', // a-tooltip 提示内容
            },
          },
          deviceLetFile: {
            show: hasPermission('batch:device:let'),
            text: $t('iot.device.bsBatch.iotBsBatchButten.deviceLetName'),
            icon: 'ant-design:cloud-upload',
            click() {
              deviceImport(2,props);
            },
          },
          deviceRenewFile: {
            show: hasPermission('batch:device:renew'),
            text: $t('iot.device.bsBatch.iotBsBatchButten.deviceRenewName'),
            icon: 'ant-design:cloud-upload',
            click() {
              deviceImport(3,props);
            },
          },
        },
        form: {
          wrapper: {
            is: 'a-modal',
          },
        },
      },
      table: {
        striped: true,
        rowKey: 'id',
        rowSelection: {
          type: 'checkbox',
          selectedRowKeys,
          onChange: (changed: string[]) => {
            selectedRowKeys.value = changed;
          },
        },
      },
      rowHandle: {
        width: '200px',
        buttons: {
          edit: {
            show: hasPermission('iot:iot:iotBsBatch:edit'),
          },
          view: {
            show: true,
          },
          remove: {
            show: hasPermission('iot:iot:iotBsBatch:delete'),
          },
          copy: {
            show: hasPermission('iot:iot:iotBsBatch:copy'),
          },
        },
      },
      columns: {
        ...indexColumn(props.crudExpose),
        name: {
          title: $t('iot.device.bsBatch.iotBsBatch.name'),
          type: 'text',
          search: { show: true },
          addForm: {
            show: true,
            value: '0',
          },
          editForm: { show: true },
          form: {
            component: {},
          },
          column: {
            show: true,
          },
        },
        letStatus: {
          title: $t('iot.device.bsBatch.iotBsBatch.letStatus'),
          type: 'dict-radio',
          dict: backendDict(DictEnum.IotBatchStauts),
          search: { show: true },
          column: {
            show: true,
          },
        },
        fileUrl: {
          title: $t('iot.device.bsBatch.iotBsBatch.fileUrl'),
          type: 'a',
          search: { show: true },
          addForm: {
            show: true,
          },
          editForm: { show: true },
          form: {
            component: {},
          },
          column: {
            show: true,
          },
        },
        bsType: {
          title: $t('iot.device.bsBatch.iotBsBatch.bsType'),
          type: 'dict-radio',
          dict: backendDict(DictEnum.IotBatchType),
          search: { show: true },
          column: {
            show: true,
          },
        },
        excCount: {
          title: $t('iot.device.bsBatch.iotBsBatch.excCount'),
          type: 'text',
          search: { show: true },
          addForm: {
            show: true,
            value: '0',
          },
          editForm: { show: true },
          form: {
            component: {},
          },
          column: {
            show: true,
          },
        },
        errorMes: {
          title: $t('iot.device.bsBatch.iotBsBatch.errorMes'),
          type: 'text',
          search: { show: true },
          addForm: {
            show: true,
          },
          editForm: { show: true },
          form: {
            component: {},
          },
          column: {
            show: true,
          },
        },
        remark: {
          title: $t('iot.device.bsBatch.iotBsBatch.remark'),
          type: 'textarea',
          search: { show: true },
          addForm: {
            show: true,
          },
          editForm: { show: true },
          form: {
            component: {},
          },
          column: {
            show: true,
          },
        },
        ...createdTimeColumn({}),
      },
    },
  };
}

export const frontRules = (
  _crudExpose: CrudExpose,
  _mode: ActionEnum,
): FormRulesExt => {
  return {};
};
